package com.hk.commons.converters;

import com.hk.commons.util.date.DatePattern;
import com.hk.commons.util.date.DateTimeUtils;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import java.time.*;
import java.time.format.DateTimeFormatter;

/**
 * @author Kevin
 * @date 2019年8月30日上午10:39:23
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class Jsr310Converters {


    public static class StringToLocalDateTimeConverter extends StringGenericConverter<LocalDateTime> {

        private static final DatePattern[] datePatterns = new DatePattern[]{DatePattern.YYYY_MM_DD_HH_MM_SS,
                DatePattern.YYYY_MM_DD_HH_MM,
                DatePattern.YYYY_MM_DD_HH_MM_CN, DatePattern.YYYY_MM_DD_HH_MM_EN,
                DatePattern.YYYY_MM_DD_HH_MM_SS_EN, DatePattern.YYYY_MM_DD_HH_MM_SS_CN};

        private StringToLocalDateTimeConverter() {
            super(LocalDateTime.class);
        }

        public static final StringToLocalDateTimeConverter INSTANCE = new StringToLocalDateTimeConverter();

        @Override
        protected LocalDateTime doConvert(String source) {
            for (DatePattern pattern : datePatterns) {
                try {
                    return DateTimeUtils.stringToLocalDateTime(source, pattern);
                } catch (Exception e) {
                    // ignore
                }
            }
            return null;
        }
    }

    public static class StringToLocalDateConverter extends StringGenericConverter<LocalDate> {

        private static final String[] datePatterns = new String[]{DateTimeUtils.YYYY_MM_DD,
                DateTimeUtils.YYYYMMDD,
                DateTimeUtils.YYYY_MM_DD_CN, DateTimeUtils.YYYY_MM_DD_EN};

        private StringToLocalDateConverter() {
            super(LocalDate.class);
        }

        public static final StringToLocalDateConverter INSTANCE = new StringToLocalDateConverter();

        @Override
        protected LocalDate doConvert(String source) {
            for (String pattern : datePatterns) {
                try {
                    return LocalDate.parse(source, DateTimeFormatter.ofPattern(pattern));
                } catch (Exception e) {
                    // ignore
                }
            }
            return null;
        }
    }

    public static class StringToLocalTimeConverter extends StringGenericConverter<LocalTime> {

        private static final String[] datePatterns = new String[]{DateTimeUtils.HH_MM_SS,
                DateTimeUtils.HH_MM,
                DateTimeUtils.HHMMSS};

        private StringToLocalTimeConverter() {
            super(LocalTime.class);
        }

        public static final StringToLocalTimeConverter INSTANCE = new StringToLocalTimeConverter();

        @Override
        protected LocalTime doConvert(String source) {
            for (String pattern : datePatterns) {
                try {
                    return LocalTime.parse(source, DateTimeFormatter.ofPattern(pattern));
                } catch (Exception e) {
                    // ignore
                }
            }
            return null;
        }
    }

    public static class StringToYearConverter extends StringGenericConverter<Year> {

        private StringToYearConverter() {
            super(Year.class);
        }

        public static final StringToYearConverter INSTANCE = new StringToYearConverter();

        @Override
        protected Year doConvert(String source) {
            return Year.parse(source);
        }
    }

    public static class StringToYearMonthConverter extends StringGenericConverter<YearMonth> {

        private static final String[] datePatterns = new String[]{DateTimeUtils.YYYY_MM,
                DateTimeUtils.YYYY_MM_EN,
                DateTimeUtils.YYYY_MM_CN};

        private StringToYearMonthConverter() {
            super(YearMonth.class);
        }

        public static final StringToYearMonthConverter INSTANCE = new StringToYearMonthConverter();

        @Override
        protected YearMonth doConvert(String source) {
            for (String pattern : datePatterns) {
                try {
                    return YearMonth.parse(source, DateTimeFormatter.ofPattern(pattern));
                } catch (Exception e) {
                    // ignore
                }
            }
            return null;
        }
    }
}
